/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.openbrew.openbrewrf;

import com.openbrew.openbrewrf.views.BeerView;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Version;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
import org.eclipse.persistence.annotations.CascadeOnDelete;

/**
 *
 * @author chapmand
 */
@Entity
@Table(name = "FinishedGoodsInventory")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "FinishedGoodsInventory.findAll", query = "SELECT f FROM FinishedGoodsInventory f ORDER BY f.fGIDescription ASC"),
    @NamedQuery(name = "FinishedGoodsInventory.findByFgiid", query = "SELECT f FROM FinishedGoodsInventory f WHERE f.fgiid = :fgiid"),
    @NamedQuery(name = "FinishedGoodsInventory.findByFGIDescription", query = "SELECT f FROM FinishedGoodsInventory f WHERE f.fGIDescription = :fGIDescription"),
    @NamedQuery(name = "FinishedGoodsInventory.findByFGIQtyOnHand", query = "SELECT f FROM FinishedGoodsInventory f WHERE f.fGIQtyOnHand = :fGIQtyOnHand"),
    @NamedQuery(name = "FinishedGoodsInventory.findByFGIQtyAllocated", query = "SELECT f FROM FinishedGoodsInventory f WHERE f.fGIQtyAllocated = :fGIQtyAllocated"),
    @NamedQuery(name = "FinishedGoodsInventory.findByFGIQtyInProduction", query = "SELECT f FROM FinishedGoodsInventory f WHERE f.fGIQtyInProduction = :fGIQtyInProduction"),
    @NamedQuery(name = "FinishedGoodsInventory.findByFGIPriceCode", query = "SELECT f FROM FinishedGoodsInventory f WHERE f.fGIPriceCode = :fGIPriceCode"),
    @NamedQuery(name = "FinishedGoodsInventory.findByBeerAndPkg", query = "SELECT f FROM FinishedGoodsInventory f WHERE f.bBeerID.bId = ?1 AND f.fGIPackageID.packageTypeID = ?2"),
    @NamedQuery(name = "FinishedGoodsInventory.findByVersion", query = "SELECT f FROM FinishedGoodsInventory f WHERE f.version = :version"),
    @NamedQuery(name = "FinishedGoodsInventory.findAllInProduction", query = "SELECT f FROM FinishedGoodsInventory f WHERE f.bBeerID.inProduction = 'true' ORDER BY f.fGIDescription ASC"),
    @NamedQuery(name = "FinishedGoodsInventory.findAllNonProduction", query = "SELECT f FROM FinishedGoodsInventory f WHERE f.bBeerID.inProduction = 'false' ORDER BY f.fGIDescription ASC")})
public class FinishedGoodsInventory implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "FGIID")
    private Integer fgiid;
    @Basic(optional = false)
    @NotNull
    @Size(min = 0, max = 250)
    @Column(name = "FGIDescription")
    private String fGIDescription;
    @Basic(optional = false)
    @NotNull
    @Column(name = "FGIQtyOnHand")
    private long fGIQtyOnHand;
    @Basic(optional = false)
    @NotNull
    @Column(name = "FGIQtyAllocated")
    private int fGIQtyAllocated;
    @Basic(optional = false)
    @NotNull
    @Column(name = "FGIQtyInProduction")
    private int fGIQtyInProduction;
    @Basic(optional = false)
    @NotNull
    @Column(name = "FGIPriceCode")
    private float fGIPriceCode;
    @Basic(optional = false)
    @NotNull
    @Column(name = "version")
    @Version
    private long version;
    @Basic(optional = false)
    @NotNull
    @Column(name = "servingTankVolume")
    private float servingTankVolume;
    @JoinColumn(name = "FGIBeerID", referencedColumnName = "BId")
    @ManyToOne(optional = false)
    private BeerView bBeerID;
    @JoinColumn(name = "FGIPackageID", referencedColumnName = "PackageTypeID")
    @ManyToOne(optional = false)
    private PackageType fGIPackageID;
    @OneToMany(mappedBy = "kegFGI")
    private Collection<ScanTblKeg> kegCollection;
    @OneToMany(mappedBy = "salesFGIID")
    private Collection<Sales> salesCollection;
    @OneToMany(mappedBy = "retFGIID")
    private Collection<ReturnedBeer> returnedBeerCollection;
    @OneToMany(cascade=CascadeType.ALL, orphanRemoval = true)
    @CascadeOnDelete
    @JoinTable(
        name="JTFGIToInvUsage",
        joinColumns={ @JoinColumn(name="FGIID", referencedColumnName="FGIID")},
        inverseJoinColumns={ @JoinColumn(name="InvUsageID", referencedColumnName="InvUsageID", unique=true)}
    )
    private List<InventoryUsage> invUsageCollection;
    @OneToMany(cascade=CascadeType.ALL, orphanRemoval = true)
    @CascadeOnDelete
    @JoinTable(
        name="JTFGITOSpclPrice",
        joinColumns={ @JoinColumn(name="FGIID", referencedColumnName="FGIID")},
        inverseJoinColumns={ @JoinColumn(name="spid", referencedColumnName="spid", unique=true)}
    )
    private List<SpclPrice> spclPriceCollection;

    public FinishedGoodsInventory() {
    }

    public FinishedGoodsInventory(Integer fgiid) {
        this.fgiid = fgiid;
    }

    public FinishedGoodsInventory(Integer fgiid, String fGIDescription, long fGIQtyOnHand, int fGIQtyAllocated, int fGIQtyInProduction, float fGIPriceCode, long version) {
        this.fgiid = fgiid;
        this.fGIDescription = fGIDescription;
        this.fGIQtyOnHand = fGIQtyOnHand;
        this.fGIQtyAllocated = fGIQtyAllocated;
        this.fGIQtyInProduction = fGIQtyInProduction;
        this.fGIPriceCode = fGIPriceCode;
        this.version = version;
    }

    public Integer getFgiid() {
        return fgiid;
    }

    public void setFgiid(Integer fgiid) {
        this.fgiid = fgiid;
    }

    public String getFGIDescription() {
        return fGIDescription;
    }

    public void setFGIDescription(String fGIDescription) {
        this.fGIDescription = fGIDescription;
    }

    public long getFGIQtyOnHand() {
        return fGIQtyOnHand;
    }

    public void setFGIQtyOnHand(long fGIQtyOnHand) {
        this.fGIQtyOnHand = fGIQtyOnHand;
    }

    public int getFGIQtyAllocated() {
        return fGIQtyAllocated;
    }

    public void setFGIQtyAllocated(int fGIQtyAllocated) {
        this.fGIQtyAllocated = fGIQtyAllocated;
    }

    public int getFGIQtyInProduction() {
        return fGIQtyInProduction;
    }

    public void setFGIQtyInProduction(int fGIQtyInProduction) {
        this.fGIQtyInProduction = fGIQtyInProduction;
    }

    public float getFGIPriceCode() {
        return fGIPriceCode;
    }

    public void setFGIPriceCode(float fGIPriceCode) {
        this.fGIPriceCode = fGIPriceCode;
    }

    public long getVersion() {
        return version;
    }

    public void setVersion(long version) {
        this.version = version;
    }

    public BeerView getFGIBeerID() {
        return bBeerID;
    }

    public void setFGIBeerID(BeerView bBeerID) {
        this.bBeerID = bBeerID;
    }

    public PackageType getFGIPackageID() {
        return fGIPackageID;
    }

    public void setFGIPackageID(PackageType fGIPackageID) {
        this.fGIPackageID = fGIPackageID;
    }

    public List<InventoryUsage> getInvUsageCollection() {
        return invUsageCollection;
    }

    public void setInvUsageCollection(List<InventoryUsage> invUsageCollection) {
        this.invUsageCollection = invUsageCollection;
    }

    public List<SpclPrice> getSpclPriceCollection() {
        return spclPriceCollection;
    }

    public void setSpclPriceCollection(List<SpclPrice> spclPriceCollection) {
        this.spclPriceCollection = spclPriceCollection;
    }

    public float getServingTankVolume() {
        return servingTankVolume;
    }

    public void setServingTankVolume(float servingTankVolume) {
        this.servingTankVolume = servingTankVolume;
    }

    @XmlTransient
    public Collection<ScanTblKeg> getKegCollection() {
        return kegCollection;
    }

    public void setKegCollection(Collection<ScanTblKeg> kegCollection) {
        this.kegCollection = kegCollection;
    }

    @XmlTransient
    public Collection<Sales> getSalesCollection() {
        return salesCollection;
    }

    public void setSalesCollection(Collection<Sales> salesCollection) {
        this.salesCollection = salesCollection;
    }

    @XmlTransient
    public Collection<ReturnedBeer> getReturnedBeerCollection() {
        return returnedBeerCollection;
    }

    public void setReturnedBeerCollection(Collection<ReturnedBeer> returnedBeerCollection) {
        this.returnedBeerCollection = returnedBeerCollection;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (fgiid != null ? fgiid.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof FinishedGoodsInventory)) {
            return false;
        }
        FinishedGoodsInventory other = (FinishedGoodsInventory) object;
        if ((this.fgiid == null && other.fgiid != null) || (this.fgiid != null && !this.fgiid.equals(other.fgiid))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.openbrew.openbrewrf.FinishedGoodsInventory[ fgiid=" + fgiid + " ]";
    }
    
    /***************************************************************************
     * Returns the price of the FGI based on the customer selected.
     * @param custID
     * @return price of fgi.
     **************************************************************************/
    public float getPrice(final long custID){
        float price = this.getFGIPriceCode();
        if(this.getSpclPriceCollection()!=null){
            for(final SpclPrice sp : this.getSpclPriceCollection()){
                if(sp.getCustID() == (int)custID){
                    price = sp.getPrice();
                }
            }
        }
        return price;
    }
    
}
